package com.gamota.mobi.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.webkit.WebView;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.gamota.mobi.R;


public class AlertDialogManager {
	
	private Context context;
	
	public AlertDialogManager(Context context){
		this.context = context;
	}
	
	public void showGiftDialog(int messageId, int days, DialogInterface.OnClickListener onClick){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.notice);
		if(days == 0){
			builder.setMessage(R.string.gift_message);
		} else {
			builder.setMessage(String.format(context.getResources().getString(messageId), days));
		}
		builder.setPositiveButton(android.R.string.ok, onClick);
		builder.show();
	}
	
	public void showButtonsDialog(int messageId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(messageId);
		builder.setPositiveButton(android.R.string.ok, listener);
		builder.setNegativeButton(android.R.string.cancel, listener);
		builder.show();
	}
	
	public void showFileDowloadDialog(String type, int messageId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		if(type.equalsIgnoreCase("apps") || type.equalsIgnoreCase("games") || type.equalsIgnoreCase("home")){
			builder.setPositiveButton(R.string.install, listener);
			builder.setMessage(R.string.install_already_exist_file);
		} else if(type.equalsIgnoreCase("books") || type.equalsIgnoreCase("comics")){
			builder.setMessage(R.string.open_already_exist_file);
			builder.setPositiveButton(R.string.open, listener);
		}
		builder.setNegativeButton(R.string.delete_file, listener);
		builder.setNeutralButton(R.string.close, listener);
		builder.show();
	}
	
	public void showPositiveDialog(int messageId, int btnText, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(messageId);
		builder.setPositiveButton(btnText, listener);
		builder.setCancelable(true);
		builder.show();
	}

	public void showDialog(int messageId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(messageId);
		builder.setPositiveButton(android.R.string.ok, listener);
		builder.show();
	}
	
	public void showDialog(String message, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setMessage(message);
		builder.setPositiveButton(android.R.string.ok, listener);
		builder.show();
	}
	
	public void showDialogWithItems(int messageId, String items[], DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(messageId);
		builder.setItems(items, listener);
		builder.show();
	}
	
	public void showDialogWithEditText(Context context, EditText editText, int titleId, int messageId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(titleId);
		builder.setMessage(Html.fromHtml(context.getResources().getString(messageId)));
		builder.setView(editText);
		builder.setPositiveButton(R.string.unzip, listener);
		builder.setNegativeButton(R.string.cancel, listener);
		builder.show();
	}
	
	public void showDialogWithCheckbox(Context context, CheckBox checkBox, int titleId, int messageId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(titleId);
		builder.setMessage(Html.fromHtml(context.getResources().getString(messageId)));
		builder.setView(checkBox);
		checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				// TODO Auto-generated method stub
				
			}
		});
		builder.setPositiveButton(R.string.unzip, listener);
		builder.setNegativeButton(R.string.cancel, listener);
		builder.show();
	}
	
	public void showDialogWithProgress(Context context, View view, int titleId, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(titleId);
		builder.setView(view);
		builder.setNegativeButton(R.string.cancel, listener);
		builder.show();
	}
	
	public void showWebViewDialog(AQuery aq, int showAttach, Context context, String content, int titleId, String iconUrl, String name, DialogInterface.OnClickListener listener){
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(titleId);
		View view = LayoutInflater.from(context).inflate(R.layout.permission_dialog, null);
		WebView webView = (WebView) view.findViewById(R.id.permissions);
		View layoutAttach = view.findViewById(R.id.layout_attach);
		ImageView icon = (ImageView) layoutAttach.findViewById(R.id.attach_icon);
		TextView attachName = (TextView) layoutAttach.findViewById(R.id.attach_name);
		if(content == null || content.equalsIgnoreCase("null")){
			content = context.getResources().getString(R.string.no_permission);
		}
		webView.loadDataWithBaseURL(null, content.replace("\\n", "<br/>"), "text/html", "UTF-8", null);
		if(showAttach == 0){
			layoutAttach.setVisibility(View.GONE);
		} else {
			AQuery aqr = new AQuery(icon);
			layoutAttach.setVisibility(View.VISIBLE);
			aqr.id(icon).image(iconUrl, false, true, 0, R.drawable.ic_launcher);
			attachName.setText(name);
		}
		builder.setView(view);
		builder.setPositiveButton(R.string.accept, listener);
		builder.setNegativeButton(R.string.cancel, new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.show();
	}
}
